import Vuex from 'vuex'

import defaultState from './state/state'
import mutations from './mutations/mutations'
import getters from './getters/getters'
import actions from './actions/actions'

const isDev = process.env.NODE_ENV === 'development'

export default () => {
  const store = new Vuex.Store({
    strict: isDev, // 生产环境不要使用
    state: defaultState,
    mutations,
    getters,
    actions,
    plugins: [
      // 只会在生成store实例时调用一次
      store => {
        console.log('plugin invoked')
      }
    ]
  })

  // 开启vuex热更新
  if (module.hot) {
    module.hot.accept(
      [
        './state/state',
        './mutations/mutations',
        './getters/getters',
        './actions/actions'
      ],
      () => {
        const newState = require('./state/state').default
        const newMutations = require('./mutations/mutations').default
        const newGetters = require('./getters/getters').default
        const newActions = require('./actions/actions').default

        store.hotUpdate({
          state: newState,
          mutations: newMutations,
          getters: newGetters,
          actions: newActions
        })
      }
    )
  }
  return store
}
